﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Common.Extention
{
    public static class StringExtention
    {
        /// <summary>
        /// 格式化现金
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string FormatToCurent(this string str)
        {
            return string.Format("{0:c}", str);
        }
        /// <summary>
        /// 是否为空,NULL OR EMPTY
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string source)
        {
            return string.IsNullOrEmpty(source);
        }
        /// <summary>
        /// 是否为空,NULL
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNull(this object source)
        {
            return source == null;
        }
        /// <summary>
        /// 格式化字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Formats(this string source, params object[] args)
        {
            return string.Format(source, args);
        }
        /// <summary>
        /// 比较字符串是否相等，不区分大小写
        /// </summary>
        /// <param name="source"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsEquals(this string source, object obj)
        {
            if (obj == null)
            {
                return false;
            }
            return source.IsEquals(obj.ToString());
        }
        /// <summary>
        /// 比较字符串是否相等，不区分大小写
        /// </summary>
        /// <param name="source"></param>
        /// <param name="strB"></param>
        /// <returns></returns>
        public static bool IsEquals(this string source, string strB)
        {
            return source != null && strB != null && string.Compare(source, strB, true) == 0;
        }
        /// <summary>
        /// 拆分字符串，并删除分拆后为空的字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string[] Split(this string source, string separator = ",")
        {
            return source.Split(separator, StringSplitOptions.RemoveEmptyEntries);
        }
        /// <summary>
        /// 拆分字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string[] Split(this string source, string separator, StringSplitOptions options)
        {
            if (source.IsNull())
            {
                return new string[0];
            }
            return source.Split(new string[] { separator }, options);
        }
        /// <summary>
        /// 拆分字符串，转化为list
        /// </summary>
        /// <param name="str"></param>
        /// <param name="separ"></param>
        /// <returns></returns>
        public static List<string> ToStringList(this string source, string separator = ",")
        {
            if (source.IsNull())
            {
                return new List<string>();
            }
            return source.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }
}
